/**
 * 拖动弹窗
 * @param movingPage 弹窗本体DOM对象
 * @param pageTitle 弹窗标题（拖动区域）DOM对象
 */
export default function (movingPage, pageTitle) {
  //鼠标拖动弹窗事件参数
  let pageInfo = {
    beforePageX: 0,
    beforePageY: 0,
    beforePopWidth: 0,
    beforePopHeight: 0,
    afterAxisX: 0,
    afterAxisY: 0,
    frameDiv:null,
    height: movingPage.clientHeight + 'px',
    width: movingPage.clientWidth + 'px',
    left: movingPage.offsetLeft + 'px',
    top: movingPage.offsetTop + 'px',
    bigScreen: false,
  };


  // 鼠标进入右下角区域时的光标样式
  movingPage.onmouseover = (evt) => {
    movingPage.onmousemove = (evt2) => {
      if (
        evt2.clientY <= movingPage.offsetTop + movingPage.clientHeight - 20 &&
        evt2.clientY >= movingPage.offsetTop - 20 &&
        evt2.clientX <= movingPage.offsetLeft + 20 + movingPage.clientWidth &&
        evt2.clientX >= movingPage.offsetLeft - 20 + movingPage.clientWidth
      ) {
        movingPage.style.cursor = "ew-resize"
      } else


        if (
          evt2.clientY <= movingPage.offsetTop + movingPage.clientHeight - 20 &&
          evt2.clientY >= movingPage.offsetTop - 20 &&
          evt2.clientX <= movingPage.offsetLeft + 20 &&
          evt2.clientX >= movingPage.offsetLeft - 20
        ) {
          movingPage.style.cursor = "ew-resize"
        } else

          if (
            evt2.clientY <= movingPage.offsetTop + pageTitle.clientHeight &&
            evt2.clientY >= movingPage.offsetTop &&
            evt2.clientX <= movingPage.offsetLeft + movingPage.clientWidth - 50 &&
            evt2.clientX >= movingPage.offsetLeft + 10
          ) {
            movingPage.style.cursor = "move"
          } else
            // 鼠标进入右下角
            if (
              evt2.clientY <= movingPage.offsetTop + movingPage.clientHeight + 10 &&
              evt2.clientY >= movingPage.offsetTop + movingPage.clientHeight - 10 &&
              evt2.clientX <= movingPage.offsetLeft + movingPage.clientWidth + 10 &&
              evt2.clientX >= movingPage.offsetLeft + movingPage.clientWidth - 10
            ) {
              movingPage.style.cursor = "se-resize";
            }
            //鼠标进入左下角
            else if (
              evt2.clientY <= movingPage.offsetTop + movingPage.clientHeight + 10 &&
              evt2.clientY >= movingPage.offsetTop + movingPage.clientHeight - 10 &&
              evt2.clientX <= movingPage.offsetLeft + 10 &&
              evt2.clientX >= movingPage.offsetLeft - 10
            ) {
              movingPage.style.cursor = "sw-resize";
            } else {
              movingPage.style.cursor = "auto";
            }
    };
    //鼠标离开区域，停止监听
    movingPage.onmouseout = () => {
      movingPage.onmousemove = null;
      movingPage.onmouseout = null;
    };
  };

  // 鼠标点下时对应操作
  pageTitle.onmousedown = (evt) => {
    pageInfo.beforePageX = evt.clientX - movingPage.offsetLeft;
    pageInfo.beforePageY = evt.clientY - movingPage.offsetTop;
    pageInfo.beforePopWidth = movingPage.clientWidth;
    pageInfo.beforePopHeight = movingPage.clientHeight;
    // 拖动窗体移动
    document.onmousemove = (evt2) => {
      pageInfo.afterAxisX = evt2.clientX;
      pageInfo.afterAxisY = evt2.clientY;
      let topChange = pageInfo.afterAxisY - pageInfo.beforePageY;
      let leftChange = pageInfo.afterAxisX - pageInfo.beforePageX;
      let topLimit = 0;
      if (
        topChange >= topLimit &&
        topChange <= window.innerHeight - movingPage.clientHeight &&
        leftChange >= 0 &&
        leftChange < window.innerWidth - movingPage.clientWidth
      ) {
        //窗体没有触及边缘时移动
        movingPage.style.top = topChange + "px";
        movingPage.style.left = leftChange + "px";
      } else {
        //让窗体在边缘也能丝滑移动
        topChange =
          topChange < topLimit
            ? topLimit
            : topChange > window.innerHeight - movingPage.clientHeight
              ? window.innerHeight - movingPage.clientHeight
              : topChange;
        leftChange =
          leftChange < 0
            ? 0
            : leftChange > window.innerWidth - movingPage.clientWidth
              ? window.innerWidth - movingPage.clientWidth
              : leftChange;
        movingPage.style.top = topChange + "px";
        movingPage.style.left = leftChange + "px";
      }
    };
  };
  pageTitle.ondblclick = (evt) => {
    console.log(pageInfo,'xinxi')
    // if (pageInfo.bigScreen) {
    //   movingPage.style.height =pageInfo.height
    //   movingPage.style.width =pageInfo.width
    //   movingPage.style.left = pageInfo.left
    //   movingPage.style.top = pageInfo.top
    // } else {
    //   movingPage.style.left = 0
    //   movingPage.style.top = 0
    //   movingPage.style.height = document.body.clientHeight + "px";
    //   movingPage.style.width = document.body.clientWidth + "px";
    // }
    // pageInfo.bigScreen = !pageInfo.bigScreen
  };
function add(){
  if(!pageInfo.frameDiv){
    pageInfo.frameDiv = document.createElement("div");//创建一个标签
    movingPage.append(pageInfo.frameDiv)
    pageInfo.frameDiv.className = "divclass"; //给创建的div设置class；
    pageInfo.frameDiv.style.position = 'absolute';
    pageInfo.frameDiv.style.top = '44px';
    pageInfo.frameDiv.style.left = '10px';
    pageInfo.frameDiv.style.right = '10px';
    pageInfo.frameDiv.style.bottom = '10px';
    pageInfo.frameDiv.style.background = '#ffffff40';
  }

}
  

  // 鼠标点下时对应操作
  movingPage.onmousedown = (evt) => {
  
   
  


    pageInfo.beforePageX = evt.clientX - movingPage.offsetLeft;
    pageInfo.beforePageY = evt.clientY - movingPage.offsetTop;
    pageInfo.beforePopWidth = movingPage.clientWidth;
    pageInfo.beforePopHeight = movingPage.clientHeight;
    //点击拖动右下角缩放
    if (
      evt.clientY <= movingPage.offsetTop + movingPage.clientHeight + 10 &&
      evt.clientY >= movingPage.offsetTop + movingPage.clientHeight - 10 &&
      evt.clientX <= movingPage.offsetLeft + movingPage.clientWidth + 10 &&
      evt.clientX >= movingPage.offsetLeft + movingPage.clientWidth - 10
    ) {
      // 消除原本的right属性对缩放的干扰
      movingPage.style.left = movingPage.offsetLeft + "px";
      movingPage.style.top = movingPage.offsetTop + "px";
      // 移动时缩放
      document.onmousemove = (evt2) => {
        add()
        pageInfo.afterAxisX =
          evt2.clientX > window.innerWidth
            ? window.innerWidth
            : evt2.clientX < 0
              ? 0
              : evt2.clientX;
        pageInfo.afterAxisY =
          evt2.clientY > window.innerHeight
            ? window.innerHeight
            : evt2.clientY < 0
              ? 0
              : evt2.clientY;
        let heightChange =
          pageInfo.afterAxisY - evt.clientY + pageInfo.beforePopHeight;
        let widthChange =
          pageInfo.afterAxisX - evt.clientX + pageInfo.beforePopWidth;
        // heightChange =
        //   heightChange < 200 ? 200 : heightChange > 800 ? 800 : heightChange;
        // widthChange =
        //   widthChange < 200 ? 200 : widthChange > 1000 ? 1000 : widthChange;
        movingPage.style.height = heightChange + "px";
        movingPage.style.width = widthChange + "px";
      };
    } else if (
      evt.clientY <= movingPage.offsetTop + movingPage.clientHeight + 10 &&
      evt.clientY >= movingPage.offsetTop + movingPage.clientHeight - 10 &&
      evt.clientX <= movingPage.offsetLeft + 10 &&
      evt.clientX >= movingPage.offsetLeft - 10
    ) {
      // 消除原本的left属性对缩放的干扰
      movingPage.style.top = movingPage.offsetTop + "px";
      movingPage.style.right =
        window.innerWidth -
        movingPage.offsetLeft -
        movingPage.clientWidth +
        "px";
      movingPage.style.left = "auto";
      // 移动时缩放
      document.onmousemove = (evt2) => {
        add()
        pageInfo.afterAxisX =
          evt2.clientX > window.innerWidth
            ? window.innerWidth
            : evt2.clientX < 0
              ? 0
              : evt2.clientX;
        pageInfo.afterAxisY =
          evt2.clientY > window.innerHeight
            ? window.innerHeight
            : evt2.clientY < 0
              ? 0
              : evt2.clientY;
        let heightChange =
          pageInfo.afterAxisY - evt.clientY + pageInfo.beforePopHeight;
        let widthChange =
          evt.clientX - pageInfo.afterAxisX + pageInfo.beforePopWidth;
        movingPage.style.height = heightChange + "px";
        movingPage.style.width = widthChange + "px";
      };
    } else if (evt.clientY <= movingPage.offsetTop + movingPage.clientHeight - 20 &&
      evt.clientY >= movingPage.offsetTop - 20 &&
      evt.clientX <= movingPage.offsetLeft + 20 &&
      evt.clientX >= movingPage.offsetLeft - 20) {
      // movingPage.style.top = movingPage.offsetTop + "px";
      movingPage.style.right =
        window.innerWidth -
        movingPage.offsetLeft -
        movingPage.clientWidth +
        "px";
      movingPage.style.left = "auto";
      // 移动时缩放
      document.onmousemove = (evt2) => {
        add()
        pageInfo.afterAxisX =
          evt2.clientX > window.innerWidth
            ? window.innerWidth
            : evt2.clientX < 0
              ? 0
              : evt2.clientX;
        let widthChange =
          evt.clientX - pageInfo.afterAxisX + pageInfo.beforePopWidth;
        movingPage.style.width = widthChange + "px";
      };
    } if (
      evt.clientY <= movingPage.offsetTop + movingPage.clientHeight - 20 &&
      evt.clientY >= movingPage.offsetTop - 20 &&
      evt.clientX <= movingPage.offsetLeft + 20 + movingPage.clientWidth &&
      evt.clientX >= movingPage.offsetLeft - 20 + movingPage.clientWidth
    ) {
      movingPage.style.left = movingPage.offsetLeft + "px";
      // movingPage.style.top = movingPage.offsetTop + "px";
      // 移动时缩放
      document.onmousemove = (evt2) => {
        add()
        pageInfo.afterAxisX =
          evt2.clientX > window.innerWidth
            ? window.innerWidth
            : evt2.clientX < 0
              ? 0
              : evt2.clientX;
        pageInfo.afterAxisY =
          evt2.clientY > window.innerHeight
            ? window.innerHeight
            : evt2.clientY < 0
              ? 0
              : evt2.clientY;
        let widthChange =
          pageInfo.afterAxisX - evt.clientX + pageInfo.beforePopWidth;
        movingPage.style.width = widthChange + "px";
      }
    }
    document.onmouseup = () => {
      document.onmousemove = null;
      document.onmouseup = null;
      if(pageInfo.frameDiv){
        pageInfo.frameDiv.remove()
        pageInfo.frameDiv=null
      }
      
    };
  };
}
